﻿using System;
using System.Collections.Generic;
using FluentNHibernate.Mapping;

namespace MVVMtutorialAdvanced.Web
{
	public class Book : EntityBase<Book>
	{
		public Book()
		{
			_bookAuthors = new List<BookAuthor>();
		}

		//public virtual int Id { get; set; }
		public virtual string Title { get; set; }
		public virtual decimal Price { get; set; }
		private readonly IList<BookAuthor> _bookAuthors;
		public virtual IList<BookAuthor> BookAuthors { get { return _bookAuthors; } }
		public virtual bool IsOutOfStock { get; set; }
		public virtual DateTime? Release { get; set; }
		public virtual Country Country { get; set; }
		public virtual byte[] Cover { get; set; }

		public virtual void AddAuthor(Author author)
		{
			var bookAuthor = new BookAuthor();
			bookAuthor.Book = this;
			bookAuthor.Author = author;
			BookAuthors.Add(bookAuthor);
		}
	}

	public class BookMap : ClassMap<Book>
	{
		public BookMap()
		{
			Id(x => x.Id);
			Map(x => x.Title);
			Map(x => x.Price);
			HasMany(x => x.BookAuthors)
				.Inverse()
				.Cascade.AllDeleteOrphan();
			Map(x => x.IsOutOfStock);
			Map(x => x.Release);
			References(x => x.Country)
				.Not.Nullable();
			Map(x => x.Cover);
		}
	}
}